﻿using ProductMonitor.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ProductMonitor.MyUserControl
{
    /// <summary>
    /// RaderUC.xaml 的交互逻辑
    /// </summary>
    public partial class RaderUC : UserControl
    {
        public RaderUC()
        {
            InitializeComponent();
            SizeChanged += OnSizeChanged;
        }

        private void OnSizeChanged(object sender, SizeChangedEventArgs e)
        {
            Drag();
        }

        public ObservableCollection<RaderModel> ItemSource
        {
            get { return (ObservableCollection<RaderModel>)GetValue(ItemSourceProperty); }
            set { SetValue(ItemSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemSourceProperty =
            DependencyProperty.Register("ItemSource", typeof(ObservableCollection<RaderModel>), typeof(RaderUC));

        private void Drag()
        {
            if(ItemSource == null || ItemSource?.Count == 0)
            {
                return;
            }
            mainCanvas.Children.Clear();
            P1.Points.Clear();
            P2.Points.Clear();
            P3.Points.Clear();
            P4.Points.Clear();
            P5.Points.Clear();

            //画布大小设定
            double size = Math.Min(RenderSize.Width, RenderSize.Height);
            LayGrid.Height = size;
            LayGrid.Width = size;
            double Raduis = size / 2;

            double step = 360.0 / ItemSource.Count;

            for (int i = 0; i < ItemSource.Count; i++)
            {
                //以radius为原点
                double x = (Raduis - 20) * Math.Cos((step * i + 90) * Math.PI / 180);//左加右减，cos向左移动π/2
                double y = (Raduis - 20) * Math.Sin((step * i - 90) * Math.PI / 180);//为啥要-90，坐标系y轴上方最小，向下递增
                P1.Points.Add(new Point(Raduis+x, Raduis + y));
                P2.Points.Add(new Point(Raduis+x*0.75, Raduis + y*0.75));
                P3.Points.Add(new Point(Raduis+x*0.5, Raduis + y*0.5));
                P4.Points.Add(new Point(Raduis+x*0.25, Raduis + y*0.25));

                P5.Points.Add(new Point(Raduis + x * ItemSource[i].Value*0.01,Raduis + y * ItemSource[i].Value * 0.01));

                //文字处理
                TextBlock txt = new TextBlock();
                txt.Width = 60;
                txt.FontSize = 10;
                txt.TextAlignment = TextAlignment.Center;
                txt.Text = ItemSource[i].ItemName;
                txt.Foreground = new SolidColorBrush(Color.FromArgb(100,255,255,255));
                txt.SetValue(Canvas.LeftProperty,Raduis+(Raduis-10) * Math.Cos((step * i + 90) * Math.PI / 180) - 30);
                txt.SetValue(Canvas.TopProperty,Raduis+(Raduis-10) * Math.Sin((step * i - 90) * Math.PI / 180) - 7);
                mainCanvas.Children.Add(txt);
            }

        }
    }
}
